﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Things.Common;
using Things.ThingsWeb;
using com.yeepay.icc;
using System.Text;

namespace Things.Web.PersonalCenter
{
    public partial class OrderPlanDefault : PageUtility
    {
        protected Things.BLL.GoodsOrders goBLL = new BLL.GoodsOrders();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckBuyer();
                BindData();
            }
        }

        public void CheckBuyer()
        {
            if (Session[ConstantString.SessionCurrentBuyerAccount] == null)
            {
                Session[ConstantString.SessionSourceURL] = Context.Request.Url;
                Response.Redirect(ConstantString.LoginPageURL);
            }
        }

        protected void BindData()
        {
            Things.Model.Buyer buyer = Session[ConstantString.SessionCurrentBuyerAccount] as Things.Model.Buyer;
            if (buyer !=null)
            {
                Things.OrderPlan.OrderPlanRepository rep = new Things.OrderPlan.OrderPlanRepository();
                List<Things.OrderPlan.Models.OrderPlan> opSet = rep.FindAll(buyer.Account);

                if (opSet != null && opSet.Count>0)
                {
                    gridView.Visible = true;
                    Label1.Visible = false;
                    gridView.DataSource = opSet;
                    gridView.DataBind();
                }
                else
                {
                    gridView.Visible = false;
                    Label1.Visible = true;
                }
            }
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string strGUID = gridView.DataKeys[e.Row.DataItemIndex - gridView.PageIndex*gridView.PageSize].Value.ToString();
                Guid guid = Guid.Empty;
                if (Guid.TryParse(strGUID, out guid) == true)
                {
                    Things.OrderPlan.OrderPlanRepository rep = new Things.OrderPlan.OrderPlanRepository();
                    Things.OrderPlan.Models.OrderPlan goModel = rep.Find(guid);
                                        
                    if (goModel != null)
                    {
                        
                        // 付款
                        LinkButton lbtPay = e.Row.FindControl("lbtPay") as LinkButton;
                        if (lbtPay != null)
                        {
                            lbtPay.Visible = goModel.Status == ConstantString.GoodsOrderSatus.未付款.ToString();
                            lbtPay.CommandArgument = guid.ToString();
                        }

                        // 取消订单
                        LinkButton lbtCancel = e.Row.FindControl("lbtCancel") as LinkButton;
                        if (lbtCancel != null)
                        {
                            lbtCancel.Visible = goModel.Status == ConstantString.GoodsOrderSatus.未付款.ToString();
                            lbtCancel.CommandArgument = guid.ToString();
                        }

                        // 修改订单
                        LinkButton lbtView = e.Row.FindControl("lbtView") as LinkButton;
                        if (lbtView != null)
                        {
                            //lbtView.Visible = goModel.Status == ConstantString.GoodsOrderSatus.已付款.ToString();
                            //lbtView.PostBackUrl = "~/GoodsOrders/ViewOrder.aspx?ID=" + guid.ToString();
                        }

                        
                    }
                }

            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            
        }

        protected void gridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            
        }

        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strGUID = e.CommandArgument.ToString();
            Guid guid = Guid.Empty;
            if (Guid.TryParse(strGUID, out guid) == true)
            {
                Things.OrderPlan.OrderPlanRepository rep = new Things.OrderPlan.OrderPlanRepository();

                Things.OrderPlan.Models.OrderPlan goModel = rep.Find(guid);

                string command = e.CommandName;
                switch (command)
                {
                    case "PayOrder":
                        // 付款
                        if (goModel != null)
                        {
                            string url = HttpContext.Current.Request.Url.AbsoluteUri;
                            // http://localhost:1302/TESTERS/Default6.aspx

                            string path = HttpContext.Current.Request.Url.AbsolutePath;
                            // /TESTERS/Default6.aspx

                            string hostURL = url.Remove(url.IndexOf(path), path.Length);

                            string orderPlanName = GetOrderPlanName(goModel);

                            Pay(goModel.OrderPlanNumber.ToString(), goModel.TotalPrice.ToString(), orderPlanName, "", "", hostURL + "/GoodsOrders/Callback.aspx", "");
                           
                        }
                        break;
                    case "CancelOrder":
                        if (goModel != null)
                        {
                            goModel.Status = ConstantString.GoodsOrderSatus.交易关闭.ToString();
                            rep.Restore(goModel);

                            BindData();
                        }
                        break;
                    case "ViewOrder":
                        if (goModel != null)
                        {
                            goModel.Status = ConstantString.GoodsOrderSatus.交易关闭.ToString();
                            rep.Restore(goModel);

                            BindData();
                        }
                        break;

                    default:
                        break;
                }
            }            
        }

        protected string GetReceiverName(object obj)
        {
            string result = string.Empty;

            try
            {
                string temp = obj.ToString();
                result = temp.Split(new string[] { ", " }, StringSplitOptions.None)[0];
            }
            catch
            {
            }

            return result;
        }


        protected string GetOrderPlanName(Things.OrderPlan.Models.OrderPlan model)
        {
            string result = "";

            if (model != null)
            {
                if (model.SaleSetId == Guid.Empty)
                {
                    result = "自由订购";
                }
                else
                {
                    Things.OrderPlan.SaleSetRepository rep = new Things.OrderPlan.SaleSetRepository();
                    Things.OrderPlan.Models.SaleSet saleSet = rep.Find(model.SaleSetId);
                    if (saleSet != null)
                    {
                        result = saleSet.Caption;
                    }
                }
            }

            return result;
        }

        

    }
}